package com.ciwong.epaper.modules.bookstore.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.bookstore.bean.StudentGroupBean;
import com.ciwong.epaper.modules.me.ui.MembersActivity;

public class StuGridViewAdapter extends BaseAdapter {

    private Context mContext;

    private StudentGroupBean studentCheckBean;

    private MembersAdapter mAdapter;


    public StuGridViewAdapter(Context context, StudentGroupBean studentCheckBean, MembersAdapter mAdapter) {
        this.mContext = context;
        this.studentCheckBean = studentCheckBean;
        this.mAdapter = mAdapter;
    }

    @Override
    public int getCount() {
        return studentCheckBean.getList().size();
    }

    @Override
    public Object getItem(int i) {
        return studentCheckBean.getList().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (null == view) {
            view = View.inflate(mContext, R.layout.activity_stu_class_child, null);
        }
        final TextView stu_class_name_text = view.findViewById(R.id.stu_class_name_text);
        final RelativeLayout stu_class_child_lay = view.findViewById(R.id.stu_class_child_lay);
        final ImageView choose_stu_img = view.findViewById(R.id.choose_stu_img);
        final ImageView is_vip_img = view.findViewById(R.id.is_vip_img);
        if (studentCheckBean.getList().get(i).getCheckStatus() == 2 || studentCheckBean.getList().get(i).getCheckStatus() == 0) {
            stu_class_child_lay.setBackgroundResource(R.drawable.stu_class_child_shape2);
            stu_class_name_text.setTextColor(mContext.getResources().getColor(R.color.tea_text_universal));
            choose_stu_img.setImageResource(R.mipmap.choose_stu_not_check);
        } else {
            stu_class_child_lay.setBackgroundResource(R.drawable.stu_class_child_shape);
            stu_class_name_text.setTextColor(mContext.getResources().getColor(R.color.tea_text_universal2));
            choose_stu_img.setImageResource(R.mipmap.choose_stu_checked);
        }

        stu_class_child_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置选中状态样式
                if (studentCheckBean.getList().get(i).getCheckStatus() == 2 || studentCheckBean.getList().get(i).getCheckStatus() == 0) {
                    studentCheckBean.getList().get(i).setCheckStatus(1);
                    studentCheckBean.setCheckCount(studentCheckBean.getCheckCount() + 1);
                    stu_class_child_lay.setBackgroundResource(R.drawable.stu_class_child_shape);
                    stu_class_name_text.setTextColor(mContext.getResources().getColor(R.color.tea_text_universal2));
                    choose_stu_img.setImageResource(R.mipmap.choose_stu_checked);
                    if (mContext instanceof MembersActivity) {
                        ((MembersActivity) mContext).setChooseStuCount();
                    }
                    if (studentCheckBean.getCheckCount() == studentCheckBean.getList().size()) {
                        ((MembersAdapter) mAdapter).notifyDataSetChanged();
                    }
                } else {
                    studentCheckBean.getList().get(i).setCheckStatus(2);
                    studentCheckBean.setCheckCount(studentCheckBean.getCheckCount() - 1);
                    stu_class_child_lay.setBackgroundResource(R.drawable.stu_class_child_shape2);
                    stu_class_name_text.setTextColor(mContext.getResources().getColor(R.color.tea_text_universal));
                    choose_stu_img.setImageResource(R.mipmap.choose_stu_not_check);
                    if (mContext instanceof MembersActivity) {
                        ((MembersActivity) mContext).setChooseStuCount();
                    }
                    if (studentCheckBean.getCheckCount() == 0) {
                        ((MembersAdapter) mAdapter).notifyDataSetChanged();
                    }
                }

            }
        });
        //TextView stu_class_count = view.findViewById(R.id.stu_class_count);
        stu_class_name_text.setText(studentCheckBean.getList().get(i).getUserName());
        if (studentCheckBean.getList().get(i).getIsVip() == 1) {
            is_vip_img.setVisibility(View.VISIBLE);
        } else {
            is_vip_img.setVisibility(View.GONE);
        }
        return view;
    }

    public void setNewData() {

    }


}
